package cn.ibizlab.odoo.core.odoo_product.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item;
import cn.ibizlab.odoo.core.odoo_product.domain.Product_pricelist_item;
import cn.ibizlab.odoo.core.odoo_product.filter.Product_pricelist_itemSearchContext;
import cn.ibizlab.odoo.core.odoo_product.service.IProduct_pricelist_itemService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[价格表明细] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Product_pricelist_itemServiceImpl implements IProduct_pricelist_itemService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iproduct_pricelist_itemClientService product_pricelist_itemClientService;

    @Override
    public Product_pricelist_item get(Integer id) {
        Iproduct_pricelist_item clientModel = product_pricelist_itemClientService.createModel();
        clientModel.setId(id);
        product_pricelist_itemClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Product_pricelist_item get(Product_pricelist_item product_pricelist_item) {
        Product_pricelist_item retproduct_pricelist_item = this.get(product_pricelist_item.getId()) ;
    	BeanCopier copier=BeanCopier.create(retproduct_pricelist_item.getClass(), product_pricelist_item.getClass(), false);
        copier.copy(retproduct_pricelist_item, product_pricelist_item, null);
        return product_pricelist_item ;
    }

    @Override
    public boolean create(Product_pricelist_item product_pricelist_item) {
        boolean bOk = false ;
        Iproduct_pricelist_item clientModel = this.convert2Model(product_pricelist_item,null);
        product_pricelist_itemClientService.create(clientModel) ;
        this.convert2Domain(clientModel,product_pricelist_item);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iproduct_pricelist_item clientModel = product_pricelist_itemClientService.createModel();
        clientModel.setId(id);
        product_pricelist_itemClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Product_pricelist_item> product_pricelist_items, int batchSize) {

                        //product_pricelist_itemClientService.createBatch(product_pricelist_items,batchSize) ;

    }

    @Override
    public void updateBatch(List<Product_pricelist_item> product_pricelist_items, int batchSize) {

        //product_pricelist_itemClientService.updateBatch(product_pricelist_items,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //product_pricelist_itemClientService.removeBatch(product_pricelist_items) ;

    }

    @Override
    public boolean update(Product_pricelist_item product_pricelist_item) {
        boolean bOk = false ;
        Iproduct_pricelist_item clientModel = this.convert2Model(product_pricelist_item,null);
        product_pricelist_itemClientService.update(clientModel) ;
        this.convert2Domain(clientModel,product_pricelist_item) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Product_pricelist_item> searchDefault(Product_pricelist_itemSearchContext context) {
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据产品种类查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_categ(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据价格表查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_base_pricelist(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据价格表查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_pricelist(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_product(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品模板查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_product_tmpl(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_company(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据币种查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_currency(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_create(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_pricelist_item> selectByOdoo_write(Integer id){
        Product_pricelist_itemSearchContext context = new Product_pricelist_itemSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_pricelist_item> list = new ArrayList<Product_pricelist_item>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item> clientModelList = product_pricelist_itemClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_pricelist_item clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_pricelist_item>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iproduct_pricelist_item convert2Model(Product_pricelist_item domain , Iproduct_pricelist_item model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = product_pricelist_itemClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getFixed_priceDirtyFlag())
            model.setFixed_price(domain.getFixed_price());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getPriceDirtyFlag())
            model.setPrice(domain.getPrice());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getBaseDirtyFlag())
            model.setBase(domain.getBase());
         if(domain.getDate_endDirtyFlag())
            model.setDate_end(domain.getDate_end());
         if(domain.getPercent_priceDirtyFlag())
            model.setPercent_price(domain.getPercent_price());
         if(domain.getPrice_roundDirtyFlag())
            model.setPrice_round(domain.getPrice_round());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getCompute_priceDirtyFlag())
            model.setCompute_price(domain.getCompute_price());
         if(domain.getPrice_min_marginDirtyFlag())
            model.setPrice_min_margin(domain.getPrice_min_margin());
         if(domain.getDate_startDirtyFlag())
            model.setDate_start(domain.getDate_start());
         if(domain.getPrice_max_marginDirtyFlag())
            model.setPrice_max_margin(domain.getPrice_max_margin());
         if(domain.getApplied_onDirtyFlag())
            model.setApplied_on(domain.getApplied_on());
         if(domain.getMin_quantityDirtyFlag())
            model.setMin_quantity(domain.getMin_quantity());
         if(domain.getPrice_surchargeDirtyFlag())
            model.setPrice_surcharge(domain.getPrice_surcharge());
         if(domain.getPrice_discountDirtyFlag())
            model.setPrice_discount(domain.getPrice_discount());
         if(domain.getPricelist_id_textDirtyFlag())
            model.setPricelist_id_text(domain.getPricelist_id_text());
         if(domain.getCurrency_id_textDirtyFlag())
            model.setCurrency_id_text(domain.getCurrency_id_text());
         if(domain.getCateg_id_textDirtyFlag())
            model.setCateg_id_text(domain.getCateg_id_text());
         if(domain.getBase_pricelist_id_textDirtyFlag())
            model.setBase_pricelist_id_text(domain.getBase_pricelist_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getProduct_tmpl_id_textDirtyFlag())
            model.setProduct_tmpl_id_text(domain.getProduct_tmpl_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getProduct_id_textDirtyFlag())
            model.setProduct_id_text(domain.getProduct_id_text());
         if(domain.getProduct_idDirtyFlag())
            model.setProduct_id(domain.getProduct_id());
         if(domain.getPricelist_idDirtyFlag())
            model.setPricelist_id(domain.getPricelist_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getBase_pricelist_idDirtyFlag())
            model.setBase_pricelist_id(domain.getBase_pricelist_id());
         if(domain.getCurrency_idDirtyFlag())
            model.setCurrency_id(domain.getCurrency_id());
         if(domain.getCateg_idDirtyFlag())
            model.setCateg_id(domain.getCateg_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getProduct_tmpl_idDirtyFlag())
            model.setProduct_tmpl_id(domain.getProduct_tmpl_id());
        return model ;
    }

    public Product_pricelist_item convert2Domain( Iproduct_pricelist_item model ,Product_pricelist_item domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Product_pricelist_item();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getFixed_priceDirtyFlag())
            domain.setFixed_price(model.getFixed_price());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getPriceDirtyFlag())
            domain.setPrice(model.getPrice());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getBaseDirtyFlag())
            domain.setBase(model.getBase());
        if(model.getDate_endDirtyFlag())
            domain.setDate_end(model.getDate_end());
        if(model.getPercent_priceDirtyFlag())
            domain.setPercent_price(model.getPercent_price());
        if(model.getPrice_roundDirtyFlag())
            domain.setPrice_round(model.getPrice_round());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getCompute_priceDirtyFlag())
            domain.setCompute_price(model.getCompute_price());
        if(model.getPrice_min_marginDirtyFlag())
            domain.setPrice_min_margin(model.getPrice_min_margin());
        if(model.getDate_startDirtyFlag())
            domain.setDate_start(model.getDate_start());
        if(model.getPrice_max_marginDirtyFlag())
            domain.setPrice_max_margin(model.getPrice_max_margin());
        if(model.getApplied_onDirtyFlag())
            domain.setApplied_on(model.getApplied_on());
        if(model.getMin_quantityDirtyFlag())
            domain.setMin_quantity(model.getMin_quantity());
        if(model.getPrice_surchargeDirtyFlag())
            domain.setPrice_surcharge(model.getPrice_surcharge());
        if(model.getPrice_discountDirtyFlag())
            domain.setPrice_discount(model.getPrice_discount());
        if(model.getPricelist_id_textDirtyFlag())
            domain.setPricelist_id_text(model.getPricelist_id_text());
        if(model.getCurrency_id_textDirtyFlag())
            domain.setCurrency_id_text(model.getCurrency_id_text());
        if(model.getCateg_id_textDirtyFlag())
            domain.setCateg_id_text(model.getCateg_id_text());
        if(model.getBase_pricelist_id_textDirtyFlag())
            domain.setBase_pricelist_id_text(model.getBase_pricelist_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getProduct_tmpl_id_textDirtyFlag())
            domain.setProduct_tmpl_id_text(model.getProduct_tmpl_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getProduct_id_textDirtyFlag())
            domain.setProduct_id_text(model.getProduct_id_text());
        if(model.getProduct_idDirtyFlag())
            domain.setProduct_id(model.getProduct_id());
        if(model.getPricelist_idDirtyFlag())
            domain.setPricelist_id(model.getPricelist_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getBase_pricelist_idDirtyFlag())
            domain.setBase_pricelist_id(model.getBase_pricelist_id());
        if(model.getCurrency_idDirtyFlag())
            domain.setCurrency_id(model.getCurrency_id());
        if(model.getCateg_idDirtyFlag())
            domain.setCateg_id(model.getCateg_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getProduct_tmpl_idDirtyFlag())
            domain.setProduct_tmpl_id(model.getProduct_tmpl_id());
        return domain ;
    }


}
